﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LetsDoIt.Controls
{
    public class CustomDialog: UserControl
    {
        const int margins = 10;

        public Label lblTitle;
        public Label lblMessage;
        public Panel pnlButtons;
        private static Dictionary<string, CustomDialog> listDialogs = new Dictionary<string, CustomDialog>();
        public Form ParentForm { get; set;}
        
        public static string ShowDialog(Form form, string title, string message, CustomDialogButton[] buttons, Image icon, int defaultButton)
        {
            string dialogID;
            do
            {
                dialogID = Guid.NewGuid().ToString();
            } while (listDialogs.ContainsKey(dialogID));

            //Form localform = form;
            CustomDialog dialog = new CustomDialog();
            dialog.ParentForm = form;
            //foreach (Control ctrl in form.Controls)
            //{
            //    ctrl.Visible = false;
            //}
            form.Controls.Add(dialog);
            listDialogs.Add(dialogID, dialog);

            dialog.Bounds = new Rectangle(margins, margins, form.Bounds.Width - 2 * margins, form.Bounds.Height - 2 * margins);
            dialog.Visible = true;
            dialog.InitializeComponent();

            dialog.lblTitle.Text = title;
            dialog.lblMessage.Text = message;
            //add icon
            //add buttons
            //set default button (by color)
            
            dialog.Show();
            Application.DoEvents();

            return dialogID;
        }

        public static void HideDialog(string dialogID)
        {
            if(listDialogs.ContainsKey(dialogID))
            {
                CustomDialog dialog = listDialogs[dialogID];
                dialog.ParentForm.Controls.Remove(dialog);
                //Form frm = dialog.ParentForm;
                dialog.ParentForm = null;
                dialog.Dispose();
                dialog = null;
                //foreach (Control ctrl in frm.Controls)
                //{
                //    ctrl.Visible = true;
                //}
                Application.DoEvents();
            }
        }

        private void InitializeComponent()
        {
            this.lblTitle = new System.Windows.Forms.Label();
            this.lblMessage = new System.Windows.Forms.Label();
            this.pnlButtons = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // lblTitle
            // 
            this.lblTitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lblTitle.BackColor = System.Drawing.Color.WhiteSmoke;
            this.lblTitle.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold);
            this.lblTitle.Location = new System.Drawing.Point(10, 10);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new System.Drawing.Size(266, 30);
            this.lblTitle.Text = "title";
            this.lblTitle.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // lblMessage
            // 
            this.lblMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lblMessage.BackColor = System.Drawing.Color.WhiteSmoke;
            this.lblMessage.Location = new System.Drawing.Point(10, 40);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(266, 64);
            this.lblMessage.Text = "message";
            this.lblMessage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // pnlButtons
            // 
            this.pnlButtons.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.pnlButtons.Location = new System.Drawing.Point(10, 107);
            this.pnlButtons.Name = "pnlButtons";
            this.pnlButtons.Size = new System.Drawing.Size(266, 48);
            this.pnlButtons.Visible = false;
            // 
            // CustomDialog
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.Controls.Add(this.pnlButtons);
            this.Controls.Add(this.lblMessage);
            this.Controls.Add(this.lblTitle);
            this.Name = "CustomDialog";
            this.Size = new System.Drawing.Size(289, 169);
            this.ResumeLayout(false);

        }
    }

    public class CustomDialogButton
    {
        public string ID { get; set; }
        public string Caption { get; set; }

        public CustomDialogButton(string id, string caption)
        {
            ID = id;
            Caption = caption;
        }
    }
}
